package com.kyexpress.common.hrinterview.provider.utils;

/**************************************************************************/
/*                                                                        */
/* 跨越速运集团有限公司版权所有                                           */
/*                                                                        */
/* PROPRIETARY RIGHTS of KYE Company are involved in the                */
/* subject matter of this material. All manufacturing, reproduction, use, */
/* and sales rights pertaining to this subject matter are governed by the */
/* license agreement. The recipient of this software implicitly accepts   */
/* the terms of the license.                                              */
/* 本软件文档资料是跨越速运集团有限公司的资产，任何人士阅读和                   */
/* 使用本资料必须获得相应的书面授权，承担保密责任和接受相应的法律约束。                 */
/*                                                                        */
/**************************************************************************/

/**
 * <pre>  文件打包下载
 * 作   者：liuyilong
 * 创建日期：2018-1-4
 * </pre>
 */


import org.slf4j.LoggerFactory;

import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.URLEncoder;
import java.util.zip.ZipOutputStream;
import java.util.zip.ZipEntry;


/**
 * 批量下载文件：
 *   使用ant.jar包中的org.apache.tools.zip.*完成压缩，
 * java原生也有java.util.zip.*但是测试了下无法搞定压缩
 * 文件内文件名的中文问题
 * @author yangcong
 *
 */
public class FileUtils {
    private static final org.slf4j.Logger logger = LoggerFactory.getLogger(FileUtils.class);
    private static final String FilePath = "D:\\";


    public String batDown(HttpServletResponse response) throws  Exception{
        //生成的ZIP文件名为Demo.zip
        String tmpFileName = "Demo.zip";
        byte[] buffer = new byte[1024];
        String strZipPath = FilePath + tmpFileName;
        ZipOutputStream out = new ZipOutputStream(new FileOutputStream(
                strZipPath));
        // 需要同时下载的两个文件result.txt ，source.txt
        File[] file1 = { new File(FilePath+"test1.txt"),
                new File(FilePath+"测试2.docx") };
        for (int i = 0; i < file1.length; i++) {
            FileInputStream fis = new FileInputStream(file1[i]);
            out.putNextEntry(new ZipEntry(file1[i].getName()));
            //设置压缩文件内的字符编码，不然会变成乱码
            //out.setEncoding("GBK");
            int len;
            // 读入需要下载的文件的内容，打包到zip文件
            while ((len = fis.read(buffer)) > 0) {
                out.write(buffer, 0, len);
            }
            out.closeEntry();
            fis.close();
        }
        out.close();
        this.downFile(response, tmpFileName);
        return null;
    }



    /**
     * 文件下载
     * @param response
     * @param str
     */
    public void downFile(HttpServletResponse response, String str) throws  Exception {
        String path = FilePath + str;
        File file = new File(path);
        if (file.exists()) {
            InputStream ins = new FileInputStream(path);
            BufferedInputStream bins = new BufferedInputStream(ins);// 放到缓冲流里面
            OutputStream outs = response.getOutputStream();// 获取文件输出IO流
            BufferedOutputStream bouts = new BufferedOutputStream(outs);
            response.setContentType("application/x-download");// 设置response内容的类型
            response.setHeader(
                    "Content-disposition",
                    "attachment;filename="
                            + URLEncoder.encode(str, "UTF-8"));// 设置头部信息
            int bytesRead = 0;
            byte[] buffer = new byte[8192];
            // 开始向网络传输文件流
            while ((bytesRead = bins.read(buffer, 0, 8192)) != -1) {
                bouts.write(buffer, 0, bytesRead);
            }
            bouts.flush();// 这里一定要调用flush()方法
            ins.close();
            bins.close();
            outs.close();
            bouts.close();
        }
    }
}


